package com.wetao.module_homework.component;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.daquexian.flexiblerichtextview.FlexibleRichTextView;
import com.wetao.lib_common.bean.response.GetAssistantQuestionList2;
import com.wetao.lib_common.bean.response.StudentExamQuestion;
import com.wetao.lib_common.finalParam.QuestionType;
import com.wetao.lib_common.greendao.model.AssistantQuestionOnline;
import com.wetao.lib_common.greendao.model.PagePictureGrid;
import com.wetao.lib_common.greendao.model.PagePictureOnline;
import com.wetao.lib_common.tsdBlePen.TsdService;
import com.wetao.lib_common.utils.FileUtils;
import com.wetao.lib_common.utils.FormulaUtil;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.view.OnMultiClickListener;
import com.wetao.lib_common.view.dialogtiplib.util.DimenUtils;
import com.wetao.module_homework.HomeworkCodeActivity;
import com.wetao.module_homework.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

import cn.hzw.doodle.DoodleColor;
import cn.hzw.doodle.DoodleOnTouchGestureListener;
import cn.hzw.doodle.DoodleParams;
import cn.hzw.doodle.DoodleTouchDetector;
import cn.hzw.doodle.DoodleView;
import cn.hzw.doodle.IDoodleListener;
import cn.hzw.doodle.core.IDoodle;
import cn.hzw.doodle.core.IDoodleItemListener;
import cn.hzw.doodle.core.IDoodleSelectableItem;
import cn.hzw.doodle.core.IDoodleTouchDetector;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class QuestionUtils {

    public interface OnChoiceListener {
        void onListener(String tempAnswer, String answer, StudentExamQuestion question);
    }

    //单选题
    public static void visualSingleChoiceContent(Context context, LinearLayout questionll, LinearLayout ll, StudentExamQuestion question, final String tempAnswer, OnChoiceListener onChoiceListener) {
        List<String> options = question.getOptions();
        if (null == options) return;
        LogUtils.d(" tempAnswer = " + tempAnswer);
        //questionll.removeAllViews();
        if (null == questionll.getChildAt(0)) {
            LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            FlexibleRichTextView questionfrtv = new FlexibleRichTextView(context);
            questionfrtv.resetTextView();
            questionfrtv.setText(new StringBuilder(question.getQuestionNo().toString()).append(". ").append(FormulaUtil.reLatex(question.getQuestion())).toString());
            questionll.addView(questionfrtv, lp0);
        } else {
            FlexibleRichTextView tempText =  ((FlexibleRichTextView) questionll.getChildAt(0));
            tempText.resetTextView();
            tempText.setText(new StringBuilder(question.getQuestionNo().toString()).append(". ").append(FormulaUtil.reLatex(question.getQuestion())).toString());
        }

        List<ImageView> listImage = new ArrayList<>();
        for (int i = 0; i < options.size(); i++) {
            String item = options.get(i);
            if (TextUtils.isEmpty(item)) continue;

            LinearLayout linearLayout = (LinearLayout) ll.getChildAt(i);
            if (null == linearLayout) {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.topMargin = (int) DimenUtils.dpToPx(20);
                lp.leftMargin = (int) DimenUtils.dpToPx(40);

                linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                linearLayout.setId(i);
                linearLayout.setTag(getOptionTag(i));

                LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                        (int) DimenUtils.dpToPx(20), (int) DimenUtils.dpToPx(20));
                params1.gravity = Gravity.CENTER_VERTICAL;
                ImageView iv = new ImageView(context);

                if (!TextUtils.isEmpty(tempAnswer) &&
                        tempAnswer.equalsIgnoreCase((String) linearLayout.getTag())) {
                    iv.setImageResource(R.drawable.homework_round_radio_select);
                } else {
                    iv.setImageResource(R.drawable.homework_round_radio_unselect);
                }
                listImage.add(iv);
                linearLayout.addView(iv, params1);

                LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp2.gravity = Gravity.CENTER_VERTICAL;
                lp2.leftMargin = (int) DimenUtils.dpToPx(20);

                FlexibleRichTextView tmpfrtv = new FlexibleRichTextView(context);
                tmpfrtv.resetTextView();
                tmpfrtv.setText(new StringBuilder((String) linearLayout.getTag()).append(". ").append(FormulaUtil.reLatex(item)).toString());
                linearLayout.addView(tmpfrtv, lp2);

                ll.addView(linearLayout, lp);
            } else {
                linearLayout.setTag(getOptionTag(i));
                ImageView iv = (ImageView) linearLayout.getChildAt(0);
                if (!TextUtils.isEmpty(tempAnswer) &&
                        tempAnswer.equalsIgnoreCase((String) linearLayout.getTag())) {
                    iv.setImageResource(R.drawable.homework_round_radio_select);
                } else {
                    iv.setImageResource(R.drawable.homework_round_radio_unselect);
                }
                listImage.add(iv);

                FlexibleRichTextView temp = (FlexibleRichTextView) linearLayout.getChildAt(1);
                temp.resetTextView();
                temp.setText(new StringBuilder((String) linearLayout.getTag()).append(". ").append(FormulaUtil.reLatex(item)).toString());
            }

            linearLayout.setOnClickListener(new OnMultiClickListener() {
                @Override
                public void onMultiClick(View v) {
                    for (int j = 0; j < listImage.size(); j++) {
                        ImageView iv = listImage.get(j);
                        if (j == v.getId()) {
                            iv.setImageResource(R.drawable.homework_round_radio_select);
                            // tempAnswer.setMyAnswer((String) v.getTag());
                            // updateQuestionAnswer(tempAnswer, (String)v.getTag(), question);
                            if (null != onChoiceListener) {
                                onChoiceListener.onListener(tempAnswer, (String) v.getTag(), question);
                            }
                        } else {
                            iv.setImageResource(R.drawable.homework_round_radio_unselect);
                        }
                    }
                }
            });
        }

        for (int i = 0; i < options.size(); i++) {
            ll.getChildAt(i).setVisibility(View.VISIBLE);
        }
        if (ll.getChildCount() > options.size()) {
            for (int i = options.size(); i < ll.getChildCount(); i++) {
                ll.getChildAt(i).setVisibility(View.GONE);
            }
        }
    }

    //多选题
    public static void visualMultiChoiceContent(Context context, LinearLayout questionll, LinearLayout ll, StudentExamQuestion question, final String tempAnswer, OnChoiceListener onChoiceListener) {
        List<String> options = question.getOptions();
        if (null == options) return;

        //questionll.removeAllViews();
        if (null == questionll.getChildAt(0)) {
            LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            FlexibleRichTextView questionfrtv = new FlexibleRichTextView(context);
            questionfrtv.resetTextView();
            questionfrtv.setText(new StringBuilder(question.getQuestionNo().toString()).append(". ").append(FormulaUtil.reLatex(question.getQuestion())).toString());
            questionll.addView(questionfrtv, lp0);
        } else {
            FlexibleRichTextView temp = ((FlexibleRichTextView) questionll.getChildAt(0));
            temp.resetTextView();
            temp.setText(new StringBuilder(question.getQuestionNo().toString()).append(". ").append(FormulaUtil.reLatex(question.getQuestion())).toString());
        }

        // ll.removeAllViews();
        Map<String, Boolean> mapMultiAnswer = new TreeMap<>();
        List<ImageView> listMultiImage = new ArrayList<>();//多选题
        for (int i = 0; i < options.size(); i++) {
            String item = options.get(i);
            if (TextUtils.isEmpty(item)) continue;

            LinearLayout linearLayout = (LinearLayout) ll.getChildAt(i);
            if (null == linearLayout) {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.topMargin = (int) DimenUtils.dpToPx(20);
                lp.leftMargin = (int) DimenUtils.dpToPx(40);

                linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                linearLayout.setId(i);
                linearLayout.setTag(getOptionTag(i));

                LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                        (int) DimenUtils.dpToPx(20), (int) DimenUtils.dpToPx(20));
                params1.gravity = Gravity.CENTER_VERTICAL;
                ImageView iv = new ImageView(context);


                if (TextUtils.isEmpty(tempAnswer)) {
                    iv.setImageResource(R.drawable.homework_round_radio_unselect);
                    mapMultiAnswer.put((String) linearLayout.getTag(), false);
                } else {
                    if (tempAnswer.contains((String) linearLayout.getTag())) {
                        iv.setImageResource(R.drawable.homework_round_radio_select);
                        mapMultiAnswer.put((String) linearLayout.getTag(), true);
                    } else {
                        iv.setImageResource(R.drawable.homework_round_radio_unselect);
                        mapMultiAnswer.put((String) linearLayout.getTag(), false);
                    }
                }

                listMultiImage.add(iv);
                linearLayout.addView(iv, params1);


                LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp2.gravity = Gravity.CENTER_VERTICAL;
                lp2.leftMargin = (int) DimenUtils.dpToPx(20);

                final FlexibleRichTextView tmpfrtv = new FlexibleRichTextView(context);
                tmpfrtv.resetTextView();
                tmpfrtv.setText(new StringBuilder((String) linearLayout.getTag()).append(". ").append(FormulaUtil.reLatex(item)).toString());
                linearLayout.addView(tmpfrtv, lp2);

                ll.addView(linearLayout, lp);
            } else {
                linearLayout.setTag(getOptionTag(i));
                ImageView iv = (ImageView) linearLayout.getChildAt(0);
                if (TextUtils.isEmpty(tempAnswer)) {
                    iv.setImageResource(R.drawable.homework_round_radio_unselect);
                    mapMultiAnswer.put((String) linearLayout.getTag(), false);
                } else {
                    if (tempAnswer.contains((String) linearLayout.getTag())) {
                        iv.setImageResource(R.drawable.homework_round_radio_select);
                        mapMultiAnswer.put((String) linearLayout.getTag(), true);
                    } else {
                        iv.setImageResource(R.drawable.homework_round_radio_unselect);
                        mapMultiAnswer.put((String) linearLayout.getTag(), false);
                    }
                }
                listMultiImage.add(iv);


                FlexibleRichTextView temp = ((FlexibleRichTextView) linearLayout.getChildAt(1));
                temp.resetTextView();
                temp.setText(new StringBuilder((String) linearLayout.getTag()).append(". ").append(FormulaUtil.reLatex(item)).toString());

            }

            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int j = 0; j < listMultiImage.size(); j++) {
                        ImageView iv = listMultiImage.get(j);
                        if (j == v.getId()) {
                            String tag = (String) v.getTag();
                            if (mapMultiAnswer.get(tag)) {
                                mapMultiAnswer.put(tag, false);
                                iv.setImageResource(R.drawable.homework_round_radio_unselect);
                            } else {
                                mapMultiAnswer.put(tag, true);
                                iv.setImageResource(R.drawable.homework_round_radio_select);
                            }
                        }
                    }
                    StringBuilder answer = new StringBuilder();
                    Iterator iter = mapMultiAnswer.entrySet().iterator();
                    while (iter.hasNext()) {
                        Map.Entry entry = (Map.Entry) iter.next();
                        if ((Boolean) entry.getValue()) {
                            answer.append((String) entry.getKey());
                        }
                    }
                    if (null != onChoiceListener) {
                        onChoiceListener.onListener(tempAnswer, answer.toString(), question);
                    }
                }
            });
        }

        for (int i = 0; i < options.size(); i++) {
            ll.getChildAt(i).setVisibility(View.VISIBLE);
        }
        if (ll.getChildCount() > options.size()) {
            for (int i = options.size(); i < ll.getChildCount(); i++) {
                ll.getChildAt(i).setVisibility(View.GONE);
            }
        }
    }

    //判断题
    public static void visualJudgeChoiceContent(Context context, LinearLayout questionll, LinearLayout ll, StudentExamQuestion question, final String tempAnswer, OnChoiceListener onChoiceListener) {
        List<String> options = question.getOptions();
        if (null == options || 0 == options.size()) {
            options = new ArrayList<>();
            options.add("对");
            options.add("错");
        }

        if (null == questionll.getChildAt(0)) {
            LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            FlexibleRichTextView questionfrtv = new FlexibleRichTextView(context);
            questionfrtv.resetTextView();
            questionfrtv.setText(new StringBuilder(question.getQuestionNo().toString()).append(". ").append(FormulaUtil.reLatex(question.getQuestion())).toString());
            questionll.addView(questionfrtv, lp0);
        } else {
            //((FlexibleRichTextView) questionll.getChildAt(0)).setText(new StringBuilder(question.getQuestionNo().toString()).append(". ").append(FormulaUtil.reLatex(question.getQuestion())).toString());
            FlexibleRichTextView temp = ((FlexibleRichTextView) questionll.getChildAt(0));
            temp.resetTextView();
            temp.setText(new StringBuilder(question.getQuestionNo().toString()).append(". ").append(FormulaUtil.reLatex(question.getQuestion())).toString());

        }

        List<ImageView> listImage = new ArrayList<>();
        for (int i = 0; i < options.size(); i++) {
            String item = options.get(i);
            if (TextUtils.isEmpty(item)) continue;

            LinearLayout linearLayout = (LinearLayout) ll.getChildAt(i);
            if (null == linearLayout) {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.topMargin = (int) DimenUtils.dpToPx(20);
                lp.leftMargin = (int) DimenUtils.dpToPx(40);

                linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                linearLayout.setId(i);
                linearLayout.setTag(i == 0 ? "T" : "F");

                LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                        (int) DimenUtils.dpToPx(20), (int) DimenUtils.dpToPx(20));
                params1.gravity = Gravity.CENTER_VERTICAL;
                ImageView iv = new ImageView(context);

                if (!TextUtils.isEmpty(tempAnswer) &&
                        tempAnswer.equalsIgnoreCase((String) linearLayout.getTag())) {
                    iv.setImageResource(R.drawable.homework_round_radio_select);
                } else {
                    iv.setImageResource(R.drawable.homework_round_radio_unselect);
                }
                listImage.add(iv);
                linearLayout.addView(iv, params1);

                LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp2.gravity = Gravity.CENTER_VERTICAL;
                lp2.leftMargin = (int) DimenUtils.dpToPx(20);

                final FlexibleRichTextView tmpfrtv = new FlexibleRichTextView(context);
                tmpfrtv.resetTextView();
                tmpfrtv.setText(new StringBuilder(options.get(i)).toString());
                linearLayout.addView(tmpfrtv, lp2);

                ll.addView(linearLayout, lp);
            } else {
                linearLayout.setTag(i == 0 ? "T" : "F");
                ImageView iv = (ImageView) linearLayout.getChildAt(0);
                if (!TextUtils.isEmpty(tempAnswer) &&
                        tempAnswer.equalsIgnoreCase((String) linearLayout.getTag())) {
                    iv.setImageResource(R.drawable.homework_round_radio_select);
                } else {
                    iv.setImageResource(R.drawable.homework_round_radio_unselect);
                }
                listImage.add(iv);

                FlexibleRichTextView temp = ((FlexibleRichTextView) linearLayout.getChildAt(1));
                temp.resetTextView();
                temp.setText(new StringBuilder(options.get(i)).toString());
            }

            linearLayout.setOnClickListener(new OnMultiClickListener() {
                @Override
                public void onMultiClick(View v) {
                    for (int j = 0; j < listImage.size(); j++) {
                        ImageView iv = listImage.get(j);
                        if (j == v.getId()) {
                            iv.setImageResource(R.drawable.homework_round_radio_select);
                            // tempAnswer.setMyAnswer((String) v.getTag());
                            // updateQuestionAnswer(tempAnswer, (String)v.getTag(), question);
                            if (null != onChoiceListener) {
                                onChoiceListener.onListener(tempAnswer, (String) v.getTag(), question);
                            }
                        } else {
                            iv.setImageResource(R.drawable.homework_round_radio_unselect);
                        }
                    }
                }
            });
        }

        for (int i = 0; i < options.size(); i++) {
            ll.getChildAt(i).setVisibility(View.VISIBLE);
        }
        if (ll.getChildCount() > options.size()) {
            for (int i = options.size(); i < ll.getChildCount(); i++) {
                ll.getChildAt(i).setVisibility(View.GONE);
            }
        }
    }


    //填空题, 简答题等点阵数据主观题
    public static void visualQuestionPenContent(Context context, LinearLayout questionll, StudentExamQuestion question, final List<PagePictureOnline> pagePictures) {
        if (null == questionll.getChildAt(0)) {
            LogUtils.i("null == questionll.getChildAt(0)");
            LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            FlexibleRichTextView questionfrtv = new FlexibleRichTextView(context);
            questionfrtv.resetTextView();
            questionfrtv.setText(new StringBuilder(question.getQuestionNo().toString()).append(". ").append(FormulaUtil.reLatex(question.getQuestion())).toString());
            questionll.addView(questionfrtv, lp0);
        } else {
            LogUtils.i("null != questionll.getChildAt(0)");
            FlexibleRichTextView temp = (FlexibleRichTextView) questionll.getChildAt(0);
            temp.resetTextView();
            temp.setText(new StringBuilder(question.getQuestionNo().toString()).append(". ").append(FormulaUtil.reLatex(question.getQuestion())).toString());
        }
    }


    private static String getOptionTag(int index) {
        String tag = "";
        switch (index) {
            case 0:
                tag = "A";
                break;
            case 1:
                tag = "B";
                break;
            case 2:
                tag = "C";
                break;
            case 3:
                tag = "D";
                break;
            case 4:
                tag = "E";
                break;
            case 5:
                tag = "F";
                break;
            case 6:
                tag = "G";
                break;
            case 7:
                tag = "H";
                break;
            case 8:
                tag = "I";
                break;
            case 9:
                tag = "J";
                break;
            case 10:
                tag = "K";
                break;
            default:
                break;
        }
        return tag;
    }
}
